
import { Injectable } from '@angular/core';
import { HttpHeaders, HttpClient, HttpParams } from '@angular/common/http';
import { Observable, of } from 'rxjs';

import { environment } from '../environments/environment';
import { Asset } from '../models/Asset';
import { AssetType } from '../models/AssetType';
import { Moment } from 'moment';
import * as moment from 'moment';

@Injectable()
export class AssetService {

  private baseUrl =  environment.apiUrl + 'api/assetinfo';
  private assetTypeUrl = environment.apiUrl + 'api/assettype';

  constructor(
    private http: HttpClient
  ) {}

  getAssets(current: number = 1, pageSize: number = environment.pageSize, options?: {assetName?: string, assetUserId?: string, assetDepartmentId?: string, assetType?: number, assetStatus?: string, orders?: string[], outQRCode?: boolean, excel?: boolean; assetCode?: string,
    assetVersion?: string, sourceComefrom?: string, assetBrand?: string,
    address?: string, seatNum?: string, snNum?: string, invoiceNum?: number, remark?: string, source?: string, repairStatus?: number, start_createDate?: Date, end_createDate?: Date, notUserId?: number}): Observable<{res: Asset[], success: boolean, total: number}> {
    const paramsObj: any = {
      pageSize: String(pageSize),
      page: String(current),
    };
    if (options) {
      if (options.assetName) paramsObj.assetName = options.assetName;
      if (options.assetUserId) paramsObj.assetUserId = options.assetUserId;
      if (options.assetDepartmentId) paramsObj.assetDepartmentId = options.assetDepartmentId;
      if (options.assetVersion)  paramsObj.assetVersion = options.assetVersion;
      if (options.sourceComefrom)  paramsObj.sourceComefrom = options.sourceComefrom;
      if (options.assetBrand)  paramsObj.assetBrand = options.assetBrand;
      if (options.address)  paramsObj.address = options.address;
      if (options.seatNum)  paramsObj.seatNum = options.seatNum;
      if (options.snNum)  paramsObj.snNum = options.snNum;
      if (options.assetCode)  paramsObj.assetCode = options.assetCode;
      if (options.invoiceNum)  paramsObj.invoiceNum = options.invoiceNum;
      if (options.remark)  paramsObj.remark = options.remark;
      if (options.source)  paramsObj.source = options.source;
      if (String(options.assetType) !== 'undefined') paramsObj.assetType = options.assetType;
      if (String(options.assetStatus) !== 'undefined') paramsObj.assetStatus = Number(options.assetStatus);
      if (options.repairStatus) paramsObj.repairStatus = options.repairStatus;
      if (options.orders) paramsObj.orders = options.orders;
      if (options.outQRCode) paramsObj.outQRCode = options.outQRCode;
      if (options.excel) paramsObj.excel = options.excel;
      if (options.notUserId) paramsObj.notUserId = options.notUserId;
      if (options.start_createDate) paramsObj.start_createDate = moment(options.start_createDate).format('YYYY-MM-DD');
      if (options.end_createDate) paramsObj.end_createDate = moment(options.end_createDate).format('YYYY-MM-DD');
    }
    const params = new HttpParams({ fromObject: paramsObj });
    return this.http.get<{res: Asset[], success: boolean, total: number}>(`${this.baseUrl}/getlist`, { params : params});
  }
  getbyassetname(current: number = 1, pageSize: number = environment.pageSize) {
    const paramsObj: any = {
      pageSize: String(pageSize),
      page: String(current),
    };
    const params = new HttpParams({ fromObject: paramsObj });
    return this.http.post<{res: Asset[], success: boolean, total: number}>(`${this.baseUrl}/getbyassetname`, { params : params});
  }
  getAssetsinfo(current: number = 1, pageSize: number = environment.pageSize, options?: {assetName?: string, assetUserId?: string, assetDepartmentId?: string, assetType?: number, assetStatus?: string, orders?: string[], outQRCode?: boolean, excel?: boolean; assetCode?: string,
    assetVersion?: string, sourceComefrom?: string, assetBrand?: string,
    address?: string, seatNum?: string, snNum?: string, rukuNum?: string, invoiceNum?: number, remark?: string, source?: string, repairStatus?: number, start_createDate?: Date, end_createDate?: Date, notUserId?: number}): Observable<{res: Asset[], success: boolean, total: number}> {
    const paramsObj: any = {
      pageSize: String(pageSize),
      page: String(current),
    };
    if (options) {
      if (options.assetName) paramsObj.assetName = options.assetName;
      if (options.assetUserId) paramsObj.assetUserId = options.assetUserId;
      if (options.assetDepartmentId) paramsObj.assetDepartmentId = options.assetDepartmentId;
      if (options.assetVersion)  paramsObj.assetVersion = options.assetVersion;
      if (options.sourceComefrom)  paramsObj.sourceComefrom = options.sourceComefrom;
      if (options.assetBrand)  paramsObj.assetBrand = options.assetBrand;
      if (options.address)  paramsObj.address = options.address;
      if (options.seatNum)  paramsObj.seatNum = options.seatNum;
      if (options.snNum)  paramsObj.snNum = options.snNum;
      if (options.assetCode)  paramsObj.assetCode = options.assetCode;
      if (options.invoiceNum)  paramsObj.invoiceNum = options.invoiceNum;
      if (options.rukuNum) paramsObj.rukuNum = options.rukuNum;
      if (options.remark)  paramsObj.remark = options.remark;
      if (options.source)  paramsObj.source = options.source;
      if (String(options.assetType) !== 'undefined') paramsObj.assetType = options.assetType;
      if (String(options.assetStatus) !== 'undefined') paramsObj.assetStatus = Number(options.assetStatus);
      if (options.repairStatus) paramsObj.repairStatus = options.repairStatus;
      if (options.orders) paramsObj.orders = options.orders;
      if (options.outQRCode) paramsObj.outQRCode = options.outQRCode;
      if (options.excel) paramsObj.excel = options.excel;
      if (options.notUserId) paramsObj.notUserId = options.notUserId;
      if (options.start_createDate) paramsObj.start_createDate = moment(options.start_createDate).format('YYYY-MM-DD');
      if (options.end_createDate) paramsObj.end_createDate = moment(options.end_createDate).format('YYYY-MM-DD');
    }
    const params = new HttpParams({ fromObject: paramsObj });
    return this.http.get<{res: Asset[], success: boolean, total: number}>(`${this.baseUrl}/getlistbyrukunum`, { params : params});
  }

  getAssetTypes(): Observable<{res: AssetType[], success: boolean}> {
    return this.http.get<{res: AssetType[], success: boolean}>(`${this.assetTypeUrl}/getlisttree`);
  }

  insertAssetType(assetType): Observable<{res: AssetType, success: boolean}> {
    return this.http.post<{res: AssetType, success: boolean}>(`${this.assetTypeUrl}/insert`, assetType);
  }

  getAsset(id: string): Observable<{res: Asset, success: boolean}> {
    return this.http.get<{res: Asset, success: boolean}>(`${this.baseUrl}/getbyid/${id}`);
  }

  getAssetType(type): Observable<{res: AssetType[] | AssetType[][], success: boolean, errorMsg: string}> {
    const params = new HttpParams({ fromObject: {
        type: String(type)
      }});
    return this.http.get<{res: AssetType[] | AssetType[][], success: boolean, errorMsg: string}>(`${this.baseUrl}/getone`, {params: params});
  }
  update(asset: Asset): Observable<{res: Asset, success: boolean}> {
    const url = `${this.baseUrl}/update`;
    return this.http
      .post<{res: Asset, success: boolean}>(url, asset);
  }

  create(asset: Asset): Observable<{success: boolean, res: Asset}> {
    delete asset.createDateShow;
    delete asset.buyDateShow;
    return this.http
      .post<{success: boolean, res: Asset}>(`${this.baseUrl}/insert`, asset);
  }

  del(id: string): Observable<{success: boolean}> {
    const url = `${this.baseUrl}/del/${id}`;
    return this.http.post<{success: boolean}>(url, {});
  }

  updateCode(id: string): Observable<{success: boolean}> {
    return this.http.get<{success: boolean}>(`${this.baseUrl}/updateqr?id=${id}`);
  }
  delbatch(rukuNum: string): Observable<{success: boolean}> {
    return this.http.post<{success: boolean}>(`${this.baseUrl}/delbatch?rukuNum=${rukuNum}`, {});
  }
  updatebatch(assets): Observable<{success: boolean}> {
    return this.http.post<{success: boolean}>(`${this.baseUrl}/updatebatch`, assets);
  }
}
